#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>

#define NUM 128
#define CMD_NUM 64

int main()
{
  char command[NUM];
  for( ; ; )
  {
    char* argv[CMD_NUM] = {NULL};
    //打印提示符
    command[0] = 0;
    printf("who@VM-20-14-centos mydir#");
    fflush(stdout);
    //获取字符串
    fgets(command,NUM, stdin);
    command[strlen(command)-1] = '\0';
    //printf("%s\n", command);
        
    //解析命令字符串
    const char* sep = " ";
    argv[0] = strtok(command, sep);
    int i = 1;
    while(argv[i] = strtok(NULL, sep))
    {
      ++i;
    }
    //检测是否是shell本身需要执行的
    if(strcmp(argv[0], "cd") == 0)
    {
      if(argv[1] != NULL)
      {
        chdir(argv[1]);
        continue;
      }
    }
    //执行第三方命令
    if(fork() == 0)
    {
      //child
      execvp(argv[0], argv);
      exit(1);
    }

     waitpid(-1, NULL, 0);

    //sleep(5);
  }
  return 0;
}
